package com.toommi.refuse.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.toommi.refuse.common.enums.CommonStatusEnum;
import com.toommi.refuse.entity.SysUser;
import com.toommi.refuse.entity.SysUserExample;
import com.toommi.refuse.enums.AdminExceptionEnum;
import com.toommi.refuse.exception.AdminException;
import com.toommi.refuse.mapper.SysPermissionMapper;
import com.toommi.refuse.mapper.SysRoleMapper;
import com.toommi.refuse.mapper.SysRolePermissionMapper;
import com.toommi.refuse.mapper.SysUserMapper;
import com.toommi.refuse.service.SysUserService;
import com.toommi.refuse.tools.Md5Util;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author yangg
 * @since 2019-07-29 10:50
 */
@Service
@AllArgsConstructor
public class SysUserServiceImpl implements SysUserService {

    private SysUserMapper sysUserMapper;

    private SysRoleMapper sysRoleMapper;

    private SysRolePermissionMapper rolePermissionMapper;

    private SysPermissionMapper permissionMapper;


    @Override
    public SysUser findUserDetail(String userName) {
        SysUserExample example = new SysUserExample();
        example.createCriteria().andUserNameEqualTo(userName);
        return sysUserMapper.selectOneByExample(example);
    }

    @Override
    public void addSysUser(SysUser sysUser) throws AdminException {

        if (StringUtils.isEmpty(sysUser.getUserName()) || sysUser.getUserName().length() > 10) {
            throw new AdminException(AdminExceptionEnum.INVlLID_USERNAME_FORMAT);
        }

        if (StringUtils.isEmpty(sysUser.getPassword())) {
            throw new AdminException(AdminExceptionEnum.INVALID_PASSWORD_NULL);
        }

        //用户名是否重复
        SysUserExample userExample = new SysUserExample();
        userExample.createCriteria().andUserNameEqualTo(sysUser.getUserName()).andStatusEqualTo(CommonStatusEnum.NORMAL.getCode());
        if (sysUserMapper.selectCountByExample(userExample) > 0) {
            throw new AdminException(AdminExceptionEnum.INVALID_USERNAME_REPEAT);
        }
        //密码加密
        sysUser.setPassword(Md5Util.encrypt(sysUser.getPassword()));

        sysUserMapper.insert(sysUser);
    }

    @Override
    public void deleteUser(long id) {
        sysUserMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Object findSysUsers(int pageNum, int pageSize, long roleId, String keyWord) {

        PageHelper.startPage(pageNum, pageSize);

        SysUserExample userExample = new SysUserExample();
        SysUserExample.Criteria criteria = userExample.createCriteria();
        criteria.andStatusEqualTo(CommonStatusEnum.NORMAL.getCode());
        if (roleId > 0) {
            criteria.andSysRoleIdEqualTo(roleId);
        }
        if (StringUtils.isNotEmpty(keyWord)) {
            criteria.andUserNameLike("%" + keyWord + "%");
        }
        List<Map<String, Object>> sysUserList = sysUserMapper.getAdminList(roleId, keyWord);
        Map<String, Object> result = new HashMap<String, Object>();
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<Map<String, Object>>(sysUserList);
        //把唯一编码
        result.put("count", pageInfo.getTotal());
        result.put("data", sysUserList);
        result.put("code", 0);
        result.put("msg", "");
        return result;
    }

    /**
     * 获取微信用户列表
     *
     * @param page
     * @param limit
     * @return
     */
    @Override
    public Object findweixinlist(int page, int limit, String name, String phone) {
        PageHelper.startPage(page, limit);

        List<Map<String, Object>> sysUserList = sysUserMapper.findweixinlist(name, phone);
        Map<String, Object> result = new HashMap<String, Object>();
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<Map<String, Object>>(sysUserList);
        //把唯一编码
        result.put("count", pageInfo.getTotal());
        result.put("data", sysUserList);
        result.put("code", 0);
        result.put("msg", "");
        return result;
    }

    /**
     * 后台获取可回收价格列表
     *
     * @param page
     * @param limit
     * @param xqid
     * @return
     */
    @Override
    public Object getprice(int page, int limit, String xqid, String date) {
        PageHelper.startPage(page, limit);

        List<Map<String, Object>> sysUserList = sysUserMapper.getprice(xqid, date);
        Map<String, Object> result = new HashMap<String, Object>();
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<Map<String, Object>>(sysUserList);
        //把唯一编码
        result.put("count", pageInfo.getTotal());
        result.put("data", sysUserList);
        result.put("code", 0);
        result.put("msg", "");
        return result;
    }

    /**
     * 获取垃圾投递记录
     *
     * @param page
     * @param limit
     * @return
     */
    @Override
    public Object getRefuseList(int page, int limit, String type, String phone, String name) {
        PageHelper.startPage(page, limit);

        List<Map<String, Object>> sysUserList = sysUserMapper.getRefuseList(type, phone, name);
        Map<String, Object> result = new HashMap<String, Object>();
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<Map<String, Object>>(sysUserList);
        //把唯一编码
        result.put("count", pageInfo.getTotal());
        result.put("data", sysUserList);
        result.put("code", 0);
        result.put("msg", "");
        return result;
    }

    /**
     * 判断垃圾是否正确
     *
     * @param request
     */
    @Override
    public void pdrefuse(HttpServletRequest request) {
        String id = request.getParameter("id");
        String status = request.getParameter("status");
        sysUserMapper.pdrefuse(id, status);
    }

    /**
     * 获取大件预约列表
     *
     * @param page
     * @param limit
     * @return
     */
    @Override
    public Object getAppointmentList(int page, int limit, String name, String phone) {
        PageHelper.startPage(page, limit);

        List<Map<String, Object>> sysUserList = sysUserMapper.getAppointmentList(name, phone);
        Map<String, Object> result = new HashMap<String, Object>();
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<Map<String, Object>>(sysUserList);
        //把唯一编码
        result.put("count", pageInfo.getTotal());
        result.put("data", sysUserList);
        result.put("code", 0);
        result.put("msg", "");
        return result;
    }

    /**
     * 获取回收员列表
     *
     * @param page
     * @param limit
     * @return
     */
    @Override
    public Object appUserList(int page, int limit, String phone, String name) {
        PageHelper.startPage(page, limit);

        List<Map<String, Object>> sysUserList = sysUserMapper.appUserList(phone, name);
        Map<String, Object> result = new HashMap<String, Object>();
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<Map<String, Object>>(sysUserList);
        //把唯一编码
        result.put("count", pageInfo.getTotal());
        result.put("data", sysUserList);
        result.put("code", 0);
        result.put("msg", "");
        return result;
    }

    /**
     * 获取垃圾投递统计记录
     *
     * @return
     */
    @Override
    public Object getRefuseTj() {
        Map<String, Object> result = new HashMap<String, Object>();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());

        String month = formatter.format(new Date());
        calendar.add(Calendar.MONTH, -1);
        String month1 = formatter.format(calendar.getTime());
        calendar.add(Calendar.MONTH, -1);
        String month2 = formatter.format(calendar.getTime());
        calendar.add(Calendar.MONTH, -1);
        String month3 = formatter.format(calendar.getTime());
        calendar.add(Calendar.MONTH, -1);
        String month4 = formatter.format(calendar.getTime());
        List<String> list = new ArrayList<>();
        list.add(month);
        list.add(month1);
        list.add(month2);
        list.add(month3);
        list.add(month4);
        List<Map<String, Object>> sysUserList = sysUserMapper.getRefuseTj(list, "normal");
        List<Map<String, Object>> sysUserList1 = sysUserMapper.getRefuseTj(list, "recyclable");
        List<Map<String, Object>> sysUserList2 = sysUserMapper.getRefuseTj(list, "large");
        result.put("month", month);
        result.put("month1", month1);
        result.put("month2", month2);
        result.put("month3", month3);
        result.put("month4", month4);
        result.put("nmonth", 0);
        result.put("nmonth1", 0);
        result.put("nmonth2", 0);
        result.put("nmonth3", 0);
        result.put("nmonth4", 0);
        result.put("rmonth", 0);
        result.put("rmonth1", 0);
        result.put("rmonth2", 0);
        result.put("rmonth3", 0);
        result.put("rmonth4", 0);
        result.put("lmonth", 0);
        result.put("lmonth1", 0);
        result.put("lmonth2", 0);
        result.put("lmonth3", 0);
        result.put("lmonth4", 0);

        result.put("mnmonth", 0);
        result.put("mnmonth1", 0);
        result.put("mnmonth2", 0);
        result.put("mnmonth3", 0);
        result.put("mnmonth4", 0);
        result.put("mrmonth", 0);
        result.put("mrmonth1", 0);
        result.put("mrmonth2", 0);
        result.put("mrmonth3", 0);
        result.put("mrmonth4", 0);
        result.put("mlmonth", 0);
        result.put("mlmonth1", 0);
        result.put("mlmonth2", 0);
        result.put("mlmonth3", 0);
        result.put("mlmonth4", 0);
        for (int i = 0; i < sysUserList.size(); i++) {
            Map<String, Object> map = sysUserList.get(i);
            String date = String.valueOf(map.get("date"));
            if (date.equals(month)) {
                result.put("nmonth", map.get("weight"));
                result.put("mnmonth", map.get("money"));
            }
            if (date.equals(month1)) {
                result.put("nmonth1", map.get("weight"));
                result.put("mnmonth1", map.get("money"));
            }
            if (date.equals(month2)) {
                result.put("nmonth2", map.get("weight"));
                result.put("mnmonth2", map.get("money"));
            }
            if (date.equals(month3)) {
                result.put("nmonth3", map.get("weight"));
            }
            if (date.equals(month4)) {
                result.put("nmonth4", map.get("weight"));
                result.put("mnmonth4", map.get("money"));
            }
        }
        for (int i = 0; i < sysUserList1.size(); i++) {
            Map<String, Object> map = sysUserList1.get(i);
            String date = String.valueOf(map.get("date"));
            if (date.equals(month)) {
                result.put("rmonth", map.get("weight"));
                result.put("mrmonth", map.get("money"));
            }
            if (date.equals(month1)) {
                result.put("rmonth1", map.get("weight"));
                result.put("mrmonth1", map.get("money"));
            }
            if (date.equals(month2)) {
                result.put("rmonth2", map.get("weight"));
                result.put("mrmonth2", map.get("money"));
            }
            if (date.equals(month3)) {
                result.put("rmonth3", map.get("weight"));
                result.put("mrmonth3", map.get("money"));
            }
            if (date.equals(month4)) {
                result.put("rmonth4", map.get("weight"));
                result.put("mrmonth4", map.get("money"));
            }
        }
        for (int i = 0; i < sysUserList2.size(); i++) {
            Map<String, Object> map = sysUserList2.get(i);
            String date = String.valueOf(map.get("date"));
            if (date.equals(month)) {
                result.put("lmonth", map.get("weight"));
                result.put("mlmonth", map.get("money"));
            }
            if (date.equals(month1)) {
                result.put("lmonth1", map.get("weight"));
                result.put("mlmonth1", map.get("money"));
            }
            if (date.equals(month2)) {
                result.put("lmonth2", map.get("weight"));
                result.put("mlmonth2", map.get("money"));
            }
            if (date.equals(month3)) {
                result.put("lmonth3", map.get("weight"));
                result.put("mlmonth3", map.get("money"));
            }
            if (date.equals(month4)) {
                result.put("lmonth4", map.get("weight"));
                result.put("mlmonth4", map.get("money"));
            }
        }
        return result;
    }

    /**
     * 指派回收员
     *
     * @param request
     */
    @Override
    public void zphsy(HttpServletRequest request) {
        String id = request.getParameter("id");
        String aid = request.getParameter("aid");
        String name = request.getParameter("name");
        String phone = request.getParameter("phone");
        sysUserMapper.zphsy(id, aid, name, phone);
    }

    /**
     * 修改用户积分
     *
     * @param request
     */
    @Override
    public void updatjf(HttpServletRequest request) {
        String id = request.getParameter("id");
        String jf = request.getParameter("jf");
        sysUserMapper.updatjf(id, jf);
    }

    /**
     * 审核回收员
     *
     * @param request
     */
    @Override
    public void updateappuser(HttpServletRequest request) {
        String id = request.getParameter("id");
        String status = request.getParameter("status");
        String remark = request.getParameter("remark");
        long time = System.currentTimeMillis();
        sysUserMapper.updateappuser(id, status,time,remark);
    }

    /**
     * 功能描述: 后台用户修改密码
     *
     * @param: [pass, newpass]
     * @return: void
     * @auther: HuangBo
     * @date: 2019/9/20 0020 下午 15:01
     */
    @Override
    public Map<String, Object> modifyPass(@RequestParam(name = "pass") String pass, @RequestParam(name = "newpass") String newpass) {
        Map<String, Object> res = new HashMap<>();
        //将输入的原密码加密
        String saltpass = Md5Util.encrypt(pass);
        String user_name = ((SysUser) SecurityUtils.getSubject().getPrincipal()).getUserName();
        //和数据库密码比对
        SysUser user = sysUserMapper.findOneByUserName(user_name);
        if (user.getPassword().equals(saltpass)) {
            newpass = Md5Util.encrypt(newpass);
            sysUserMapper.updatePassById(newpass,user.getId());
            res.put("code", 200);
            res.put("msg", "修改成功");
        } else {
            res.put("code", 201);
            res.put("msg", "原密码不正确");
        }
        return res;
    }
}
